<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable @input="getDataList()"></el-input>
      </el-form-item>
      <!-- <el-form-item> -->
      <!-- <el-button @click="getDataList()">查询</el-button> -->
      <!-- </el-form-item> -->
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
      :tree-props="{children: 'workDetailList',hasChildren:'hasWork'}"
    >
      >
      <!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
      <el-table-column type="expand" align="center">
        <template slot-scope="props">
          <el-table :data="props.row.workDetailList" style="width: 100%" max-height="250">
            <el-table-column prop="quRepoName" header-align="center" align="center" label="作业名称"></el-table-column>
            <el-table-column prop="finishCount" header-align="center" align="center" label="完成人数"></el-table-column>
            <el-table-column
              prop="unFinishCount"
              header-align="center"
              align="center"
              label="未完成人数"
            ></el-table-column>
            <el-table-column prop="finishRate" header-align="center" align="center" label="完成率">
              <template slot-scope="scope">
                <el-tag type="success">{{scope.row.finishRate}}%</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" header-align="center" label="作业状态" prop="status">
              <template slot-scope="scope">
                <el-tag type="warning" v-if="scope.row.status === 0">未开始</el-tag>
                <el-tag type="success" v-if="scope.row.status === 1">进行中</el-tag>
                <el-tag v-if="scope.row.status === 2">已完成</el-tag>
              </template>
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="openStudentWorkDetail(scope.row.workId)"
                >查看完成情况</el-button>
                <el-button type="text" size="small" @click="deleteWork(scope.row.workId)">删除作业</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>

      <el-table-column prop="name" header-align="center" align="center" label="班级名称"></el-table-column>
      <el-table-column prop="courseName" header-align="center" align="center" label="课程名称"></el-table-column>
      <el-table-column prop="departmentName" header-align="center" align="center" label="系部名称"></el-table-column>
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="pubWork(scope.row.courseId,scope.row.classCourseId)"
          >发布作业</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="发布作业" :visible.sync="dialogVisible" width="35%">
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="80px"
      >
        <el-form-item label="题库选择" prop="questionBankId">
          <el-select
            v-model="dataForm.questionBankId"
            filterable
            placeholder="选择或搜索题库"
            style="width:70%"
          >
            <el-option
              v-for="item in questionBank"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间范围" prop="datetimeRange">
          <el-date-picker
            v-model="dataForm.datetimeRange"
            type="datetimerange"
            range-separator="|"
            start-placeholder="开始时间"
            end-placeholder="截止时间"
            style="width:70%"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitWork">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="学生完成详情" :visible.sync="workDetailDialogVisible" width="50%">
      <el-table
        :data="studentWorkDetailList"
        border
        v-loading="studentWorkDetailListLoading"
        style="width: 100%"
      >
        <el-table-column prop="stuNo" header-align="center" align="center" label="学号" width="100px"></el-table-column>
        <el-table-column prop="stuName" header-align="center" align="center" label="姓名"></el-table-column>
        <el-table-column prop="trueCount" header-align="center" align="center" label="正确数"></el-table-column>
        <el-table-column prop="errCount" header-align="center" align="center" label="错题数"></el-table-column>
        <el-table-column prop="unAnswerCount" header-align="center" align="center" label="未答数"></el-table-column>
        <el-table-column prop="status" header-align="center" align="center" label="完成情况">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.status === 0">未完成</el-tag>
            <el-tag type="success" v-if="scope.row.status === 1">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" width="100" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="viewWorkDetail(scope.row.id)"
              v-if="scope.row.status === 1"
            >查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      studentWorkDetailList: [],
      studentWorkDetailListLoading: false,
      workDetailDialogVisible: false,
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      dialogVisible: false,
      questionBank: [],
      classCourseId: 0,
      workId: null,
      dataRule: {
        questionBankId: [
          { required: true, message: '题库', trigger: 'blur' }
        ],
        datetimeRange: [
          { required: true, message: '时间范围不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {

  },
  activated () {
    this.getDataList()
  },
  methods: {
    viewWorkDetail (userId) {
      this.workDetailDialogVisible = false
      this.$router.push({ name: 'manage-workResult', query: { workId: this.workId, userId: userId } })
    },
    openStudentWorkDetail (workId) {
      this.workId = workId
      this.workDetailDialogVisible = true
      this.studentWorkDetailListLoading = true
      this.$http({
        url: this.$http.adornUrl(`/workmanagement/work/getStudentWorkDetailList`),
        method: 'get',
        params: this.$http.adornParams({
          'workId': workId
        }
        )
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.studentWorkDetailList = data.list
        }
        this.studentWorkDetailListLoading = false
      })
    },
    // 删除
    deleteWork (workId) {
      let ids = [workId]
      this.$confirm(`确定对[id=${workId}]进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/workmanagement/work/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    pubWork (courseId, classCourseId) {
      this.classCourseId = classCourseId
      this.dialogVisible = true
      this.$http({
        url: this.$http.adornUrl(`/workmanagement/questionbank/getInfo`),
        method: 'get',
        params: this.$http.adornParams({
          'courseId': courseId
        }
        )
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.questionBank = data.questionBanks
        }
      })
    },
    submitWork () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/workmanagement/classcourse/submitWork`),
            method: 'post',
            data: this.$http.adornData({
              'classCourseId': this.classCourseId,
              'questionBankId': this.dataForm.questionBankId,
              'datetimeRange': this.dataForm.datetimeRange
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '发布成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.dialogVisible = false
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/workmanagement/classcourse/getMyClassList'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    }
  }
}
</script>
